<?php

require_once 'modules/content/models/Content.php';
require_once 'modules/content/models/ContentCategory.php';

class content_IndexController extends Nine_Controller_Action
{
	public function indexAction()
	{
		
		$objContent = new Models_Content();
        $objContentCategory = new Models_ContentCategory();
        
        /**
         * Get category
         */
        $categoryGid = $this->_getParam('cid',false);
        $alias = $this->_getParam('alias',false);
        if (false == $categoryGid) {
        	if (false == $alias) {
            	$this->_redirectToNotFoundPage();
        	}
        	else {
        		$cat = @reset($objContentCategory->getByColumns(array('alias=?' => $alias))->toArray());
        		$this->_request->setParam('cid', $cat['content_category_gid']);
        	}
        
        }
        else {
        	$cat = $objContentCategory->getCategoryWithParent($categoryGid);
        }
        $condition = array();
        if ('service' == $cat['template']){
            $allContents = $objContent->getAllEnabledContentByCategory($cat['content_category_gid'], $condition, 
                                                                        array('sorting ASC'));
            $i = 0;    
            foreach ($allContents as &$item) {
            	$i++;
            	$item['num'] = $i;
            	$item['url'] = Nine_Route::_("content/index/detail/id/{$item['content_gid']}",array('alias' => $item['alias']));
            }
            unset($item);

            $this->view->service = $allContents;
            $this->view->menuId = 'service';
        }
		else if ('reference' == $cat['template']){
			
			/**
			 * Get all references
			 */
		    $condition = array();
		    $numRowPerPage = Nine_Registry::getConfig("referNumberRowPerPage"); 
            $currentPage = $this->_getParam("page",1);
            $allReferences = $objContent->getAllEnabledContentByCategory($cat['content_category_gid'], $condition, 
                                                                         array('sorting ASC','content_id DESC'),
                                                                             $numRowPerPage,($currentPage - 1) * $numRowPerPage);
            foreach ($allReferences as &$item){
            	if(null != @$item['images']) {
            		$item['main_image'] = @reset(explode('||', trim($item['images'],'|| ')));
            		if(null != $item['main_image']) {
            			$tmp = @explode(":", trim($item['main_image'],': '));
            			$item['main_image'] = array(
			            			'path' => Nine_Function::getThumbImage(@$tmp[0], 300, 200,null,true),
			            			'title' => @$tmp[1]
			            			);
            		}
            	}
		        $item['url'] = Nine_Route::_("content/index/detail/id/{$item['content_gid']}",array('alias' => $item['alias']));
            }
            unset($item);
            $count = count($objContent->getAllEnabledContentByCategory($cat['content_category_gid']));
            /**
             * Assign to view
             */
            $this->setPagination($numRowPerPage, $currentPage, $count);                                                           
            $this->view->allReferences= array_chunk($allReferences,3);
            $this->view->menuId = "reference";                               
        }
		else if ('blogs' == $cat['template']){
            $condition = array();
            $numRowPerPage = Nine_Registry::getConfig("blogNumberRowPerPage"); 
            $currentPage = $this->_getParam("page",1);
            $allBlogs = $objContent->getAllEnabledContentByCategory($cat['content_category_gid'], $condition, 
                                                                         array('sorting ASC','content_id DESC'),
                                                                             $numRowPerPage,($currentPage - 1) * $numRowPerPage);
            foreach ($allBlogs as &$item){
                $item['intro_text'] = Nine_Function::subStringAtBlank(trim(strip_tags($item['intro_text'])),900);
//                $tmp = explode('||', $item['images']);
//                $item['main_image'] = Nine_Function::getThumbImage(@$tmp[0], 192, 135,null,true);
            	if(null != @$item['images']) {
            		$item['main_image'] = @reset(explode('||', trim($item['images'],'|| ')));
            		if(null != $item['main_image']) {
            			$tmp = @explode(":", trim($item['main_image'],': '));
            			$item['main_image'] = array(
			            			'path' => Nine_Function::getThumbImage(@$tmp[0], 284, 189,null,true),
			            			'title' => @$tmp[1]
			            			);
            		}
            	}
                
                $item['url'] = Nine_Route::_("content/index/detail/id/{$item['content_gid']}",array('alias' => $item['alias']));
                $item['created_date'] = date('F/d/Y',$item['created_date']);
                $item['date'] = explode('/', $item['created_date']);
            }
            unset($item);
            
            
            $count = count($objContent->getAllEnabledContentByCategory($cat['content_category_gid']));
            $this->setPagination($numRowPerPage, $currentPage, $count);                                                           

            $this->view->blog= $allBlogs;
        }
	   	else if ('partner' == $cat['template']){
            $condition = array();
            $numRowPerPage = Nine_Registry::getConfig("partnerNumberRowPerPage"); 
            $currentPage = $this->_getParam("page",1);
            $allPartner = $objContent->getAllEnabledContentByCategory($cat['content_category_gid'], $condition, 
                                                                         array('sorting ASC','content_id DESC'),
                                                                             $numRowPerPage,($currentPage - 1) * $numRowPerPage);
                                                                        
             foreach ($allPartner as &$item){
                $item['intro_text'] = Nine_Function::subStringAtBlank(trim(strip_tags($item['intro_text'])),30);
                $tmp = explode('||', $item['images']);
                $item['images_main'] = Nine_Function::getThumbImage(@$tmp[0], 275, 145,null,true);
                $item['url'] = Nine_Route::_("content/index/detail/id/{$item['content_gid']}",array('alias' => $item['alias']));
                
            }
            unset($item);
            
            $count = count($objContent->getAllEnabledContentByCategory($cat['content_category_gid']));
            $this->setPagination($numRowPerPage, $currentPage, $count);                                                           
            $this->view->partner= $allPartner;
                                                   
        }
        
        $templatePath  = Nine_Registry::getModuleName() . '/views/templates/' . ((@$cat['template'])?$cat['template']:'default');
        $templatePath .= '/index.' . Nine_Constant::VIEW_SUFFIX;
        
        $rootCatParent = $objContentCategory->getRootParent($categoryGid);
        
        $this->session->frontBanner = 'front/images/img/banner_04.jpg';
        $this->view->headTitle($cat['name']);
        
        $this->view->cat = $cat;
        
        /**
         * Render this template
         */
        $this->view->html = $this->view->render( $templatePath );
	}
	
	public function detailAction()
	{
		$objContent = new Models_Content();
        $objContentCategory = new Models_ContentCategory();
        
        $gid = $this->_getParam('id',false);
        $alias = $this->_getParam('alias',false);
        if ( false == $gid) {
        	if (false == $alias) {
				$this->_redirectToNotFoundPage();
			}
			else {
				$news = @reset($objContent->getByColumns(array('alias=?' => $alias))->toArray());
				$this->_request->setParam('id', $news['content_gid']);
			}
        }
        else {
        	$news = $objContent->getContentById($gid);
        }
        
        if (null == $news) {
            $this->_redirectToNotFoundPage();
        }
        
        $cat = @reset($objContentCategory->getByColumns(array('content_category_gid=?'=>$news['content_category_gid']))->toArray());
        if (null == $cat) {
            $this->_redirectToNotFoundPage();
        }
        
        if ('aboutus' == $cat['template']){
        	
        	$condition = array();
            $allContents = $objContent->getAllEnabledContentByCategory($cat['content_category_gid'], $condition, 
                                                                            array('sorting ASC','content_id DESC')

                                                                            );
            $i = 0;
            foreach ($allContents as &$item){
                $item['title'] = Nine_Function::subStringAtBlank(trim(strip_tags($item['title'])),86);
                $item['intro_text'] = Nine_Function::subStringAtBlank(trim(strip_tags($item['intro_text'])),265);
                @$tmp = explode('||', $item['images']);
                @$item['images'] = Nine_Function::getThumbImage(@$tmp[0], 129, 87);
                $item['url'] = Nine_Route::_("content/index/detail/id/{$item['content_gid']}",array('alias' => $item['alias']));
                $item['created_date'] = date('d/m/Y',$item['created_date']);
                
                if(6==$i){
                    $othersNews[] = $item;
                }
                else{
                    $i++;
                }
            }
            unset($item);
            
            /*
             * Testimonial 
             */
            $numRowPerPage = Nine_Registry::getConfig("testNumberRowPerPage"); 
            $currentPage = $this->_getParam("page",1);
            $allTest = $objContent->getAllEnabledContentByCategory(15, $condition,array('sorting ASC','content_id DESC'),
                                                                            $numRowPerPage,($currentPage - 1) * $numRowPerPage);
                                                                        
                                                                        
            $allTest = array_chunk($allTest, 2);
//            echo "<pre>";print_r($allTest);die;
            
            $count = count($objContent->getAllEnabledContentByCategory(15));
            $this->setPagination($numRowPerPage, $currentPage, $count);                                                           
            $this->view->test = $allTest;  
            $this->view->menuId = "aboutus"; 
//            $this->session->frontBanner = 'front/images/img/banner_01.jpg';                                                         
                                                   
        }
	   else if ('service' == $cat['template']){
	   	
	   	    $condition['exclude_content_gids'] = $news['content_gid'];
            $allSevice = $objContent->getAllEnabledContentByCategory(1, $condition, 
                                                                        array('sorting ASC'));
                                                                        
            $i = 1;                                                            
            foreach ($allSevice as &$item) {
                $i++;
                $item['num'] = $i;
                $item['url'] = Nine_Route::_("content/index/detail/id/{$item['content_gid']}",array('alias' => $item['alias']));
            }
            unset($item);
	    	$news['images'] = @explode('||', trim($news['images'],'|| '));
        	if(!empty($news['images'][1])) {
        		$news['main_image'] = $news['images'][1];
        		unset($news['images'][1]);
        		$tmp = @explode(':', trim($news['main_image'],': '));
        		$news['main_image'] = array(
        							'large' => @$tmp[0],
			            			'title' => @$tmp[1]
        							);
        }
//            echo "<pre>";print_r($allContents);die;
            $news['url'] = Nine_Route::_("content/index/detail/id/{$news['content_gid']}",array('alias' => $news['alias']));
            $this->view->service = $allSevice;
            
//            echo "<pre>"; print_r($allSevice);die;
            $this->view->menuId = "service";
//            $this->session->frontBanner = 'front/images/img/banner_01.jpg';

                                                   
        }
        else if('reference' == $cat['template']){
        	
//        	$news['popup'] = Nine_Route::_("content/index/popup/id/{$news['content_gid']}",array('alias' => $news['alias']));
        	$news['images'] = @explode('||', trim($news['images'],'|| '));
        	if(!empty($news['images'])) {
        		
        		$news['main_image'] = $news['images'][0];
        		unset($news['images'][0]);
        		$tmp = @explode(':', trim($news['main_image'],': '));
        		$news['main_image'] = array(
        							'large' => @$tmp[0],
			            			'small' => @Nine_Function::getThumbImage(@$tmp[0],320,217,null,true),
			            			'title' => @$tmp[1]
        							);
				        		
	            foreach ($news['images'] as &$item){
	            	$tmp = @explode(":", trim($item));
	            	$item = array(
	            			'large' => @$tmp[0],
	            			'small' => @Nine_Function::getThumbImage(@$tmp[0],660, 440, '#000000', false),
	            			'big' => @Nine_Function::getThumbImage(@$tmp[0],1000),
	            			'title' => @$tmp[1]
	            			);
	            }
	            unset($item);
	            
	            //$news['images'] = array_chunk($news['images'], 3);
        	}
        	if (null == trim($news['intro_text'])) {
        		$news['intro_text'] = $news['full_text'];
        	}
        	
        	$condition['exclude_content_gids'] = $news['content_gid'];
        	$numRowPerPage = Nine_Registry::getConfig("referDetailNumberRowPerPage"); 
            $currentPage = $this->_getParam("page",1);
            
//            $count = count($objContent->getAllEnabledContentByCategory($cat['content_category_gid'],$condition));
//            $this->setPagination($numRowPerPage, $currentPage, $count);                                                           
//            $this->view->referOthers = $allReferOthers;   
            $this->view->news = $news;
            $this->view->menuId = "reference";
//            echo "<pre>"; print_r($allReferOthers);die;
//            $this->session->frontBanner = 'front/images/img/banner_03.jpg';                                  
        }
	    else if('blogs' == $cat['template']){
	    	if(null != @$news['images']) {
	    		$news['main_image'] = @reset(explode('||', trim($news['images'],'|| ')));
	    		if(null != $news['main_image']) {
	    			$tmp = @explode(':', trim($news['main_image'],': '));
	    			$news['main_image'] = array(
	    								'path' => Nine_Function::getThumbImage(@$tmp[0], 269, 132,null,true),
	    								'title' => @$tmp[1]
	    								);
	    		}
	    	}
	    	
//	    	$tmp = explode('||', $news['images']);
//            if(@$tmp[1]){
//                    $news['main_image'] = Nine_Function::getThumbImage(@$tmp[1], 269, 132,null,true);
//                }else{
//                    $news['main_image'] = Nine_Function::getThumbImage(@$tmp[0], 269, 132,null,true);
//                }
                
            $news['date'] = date('F d, Y',$news['created_date']);
            
            $condition['exclude_content_gids'] = $news['content_gid'];
            $numRowPerPage = Nine_Registry::getConfig("blogDetailNumberRowPerPage"); 
            $currentPage = $this->_getParam("page",1);
            $allBlogOthers = $objContent->getAllEnabledContentByCategory($cat['content_category_gid'], $condition,array('sorting ASC','content_id DESC'),
                                                                            $numRowPerPage,($currentPage - 1) * $numRowPerPage);
                                                                        
             foreach ($allBlogOthers as &$item){
//                $item['intro_text'] = Nine_Function::subStringAtBlank(trim(strip_tags($item['intro_text'])),30);
//                $tmp = explode('||', $item['images']);
                
             	$item['images'] = @explode('||', $item['images']);
                if(!empty($item['images'])) {
                	$tmp = @explode(":", trim($item['images'][0],': '));
                	$item['main_image'] = @Nine_Function::getThumbImage(@$tmp[0],192, 135,null,true);
                }
                
//                $item['main_image'] = @Nine_Function::getThumbImage(@$tmp[0], 192, 135,null,true);
                
                $item['url'] = @Nine_Route::_("content/index/detail/id/{$item['content_gid']}",array('alias' => $item['alias']));
                $item['created_date'] = @date('F/d/Y',$item['created_date']);
                $item['date'] = @explode('/', $item['created_date']);
                
            }
            unset($item);
            $count = count($objContent->getAllEnabledContentByCategory($cat['content_category_gid'],$condition));
            $this->setPagination($numRowPerPage, $currentPage, $count);                                                           
            $this->view->blogOthers = $allBlogOthers;
//            $this->session->frontBanner = 'front/images/img/banner_01.jpg';                                      
        }
        
        $templatePath  = Nine_Registry::getModuleName() . '/views/templates/' . ((@$cat['template'])?$cat['template']:'default');
        $templatePath .= '/detail.' . Nine_Constant::VIEW_SUFFIX;
        
        /**
         * for about us
         */
        $this->view->headTitle($news['title']);
//        $rootCatParent = $objContentCategory->getRootParent($cat['content_category_gid']);
//        if (55 == $news['content_gid']) {
//            $this->view->menuId = "content_55";
//        }
//        else {
//            
//            $this->view->menuId = "content_category_{$rootCatParent['content_category_gid']}";
//        }
		if (null != @$news['meta_keywords']) {
        	$this->view->headMetaKeywords($news['meta_keywords']);
        }
        
	 	if (null != @$news['meta_description']) {
        	$this->view->headMetaDescription($news['meta_description']);
        }
        
       
        $this->view->news = $news;
        $this->view->cat = $cat;
        $this->view->html = $this->view->render( $templatePath );
		
	}
//	public function popupAction(){
//		$gid = $this->_getParam('id',false);
////		echo "<pre>";print_r($gid);die;
//        $templatePath  = Nine_Registry::getModuleName() . '/views/templates/' . ((@$cat['template'])?$cat['template']:'default');
//        $templatePath .= '/popup.' . Nine_Constant::VIEW_SUFFIX;
//	}
	
	
	
	private function _redirectToNotFoundPage()
	{
	    $this->_redirect("");
	}
}